package www.notice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import www.Action;
import www.ActionForward;
import www.notice.NoticeDTO;
import www.notice.NoticeMgr;
import www.utility.Utility;

public class NoticeUpdate implements Action {
     @Override
    public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
         System.out.println("memberupdate executed");
         
         int noticeno = Integer.parseInt(request.getParameter("noticeno"));
         String nickname = request.getParameter("nickname");
         String title = request.getParameter("title");
         String contents = request.getParameter("contents");
         String profilephoto = request.getParameter("profilephoto");
         String noticedate = request.getParameter("noticedate");
         
         title = Utility.byteToString(title);
         contents = Utility.byteToString(contents);
         nickname = Utility.byteToString(nickname);
         profilephoto = Utility.byteToString(profilephoto);
         
         request.setCharacterEncoding("euc-kr");
         
         NoticeMgr mgr = new NoticeMgr();
         NoticeDTO dto = new NoticeDTO();
         
         dto.setNoticeno(noticeno);
         dto.setNickname(nickname);
         dto.setTitle(title);
         dto.setContents(contents);
         dto.setProfilephoto(profilephoto);
         dto.setNoticedate(noticedate);
         
         JSONObject obj = new JSONObject(); 
//         JSONArray data = new JSONArray();

         
         
         //Iterator<Object> iter = list.iterator();   
         try{
             int retval = (Integer) mgr.update(dto);
             System.out.println("retval: "+retval);
         if(retval == 1){
                     //JSONObject item = new JSONObject();
//                     dto = (MemberDTO)create;
//                     
//                     item.put("id", dto.getId());
//                     item.put("introduce", Utility.toJS(dto.getIntroduce()));
//                     item.put("nickname", Utility.toJS(dto.getNickname()));
//                     item.put("profilephoto", dto.getProfilephoto());
//                     item.put("password", Utility.toJS(dto.getPassword()));
//                     item.put("email", Utility.toJS(dto.getEmail()));
//                     item.put("memlevel", Utility.toJS(dto.getMemlevel()));
                     
                  //   data.add(item);
                 obj.put("code","success");
                 obj.put("message", "데이터 수정에  성공하였습니다.");
                 //obj.put("data",data);   
               
         }else{
               obj.put("code","error");
               obj.put("message", "데이터 수정에 실패하였습니다.");
           }
         }catch(Exception e){
             System.out.println(e);
         }finally{
             
         }
             System.out.print("notice: " +  obj);
             request.setAttribute("obj", obj);
            
             ActionForward forward = new ActionForward();
             forward.setRedirect(false);  // 
             forward.setPath("/obj.jsp");
             
             return forward;
     }
}
